package com.ruoyi.factory.estimate.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.estimate.domain.PjrecordView;
import com.ruoyi.factory.estimate.domain.PjrecordViewqz;
import com.ruoyi.factory.estimate.mapper.PjrecordViewMapper;
import com.ruoyi.factory.estimate.mapper.PjrecordViewqzMapper;
import com.ruoyi.factory.estimate.service.IPjrecordViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 评价记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-16
 */
@Service
public class PjrecordViewServiceImpl implements IPjrecordViewService
{
    @Autowired
    private PjrecordViewMapper pjrecordViewMapper;

    @Autowired
    private PjrecordViewqzMapper pjrecordViewqzMapper;

    /**
     * 查询评价记录
     * 
     * @param id 评价记录主键
     * @return 评价记录
     */
    @Override
    public PjrecordView selectPjrecordViewById(Long id)
    {
        return pjrecordViewMapper.selectPjrecordViewById(id);
    }

    /**
     * 查询评价记录列表
     * 
     * @param pjrecordView 评价记录
     * @return 评价记录
     */
    @Override
    public List<PjrecordView> selectPjrecordViewList(PjrecordView pjrecordView)
    {
        return pjrecordViewMapper.selectPjrecordViewList2(pjrecordView);
    }

    /**
     * 查询评价记录列表
     *
     * @param pjrecordView 评价记录
     * @return 评价记录
     */
    @Override
    public List<PjrecordView> selectPjrecordViewListpz(PjrecordView pjrecordView)
    {
        List<PjrecordView> pjrecordViews = pjrecordViewMapper.selectPjrecordViewListpz(pjrecordView);
        Map<String,PjrecordView> hashmap = new HashMap<>();
        for (PjrecordView view : pjrecordViews) {
            String bpjpsndocname = view.getBpjpsndocname();
            PjrecordView pjrecordView1 = hashmap.get(bpjpsndocname);

            if(null == pjrecordView1){
                String relation = view.getRelation();
                //判断评价关系 根据评价关系计算得分
                if("上级".equals(relation)){
                    //得分除以人数乘以权重
                    String percount = view.getPercount();
                    String zhuanye = view.getZhuanye();
                    String chuangxin = view.getChuangxin();
                    String gongzuotaidu = view.getGongzuotaidu();
                    String xiezuonengli = view.getXiezuonengli();
                    String zhanlueshiye = view.getZhanlueshiye();
                    String lingdaoli = view.getLingdaoli();
                    if(StringUtils.isNotBlank(percount) && new BigDecimal("0").compareTo(new BigDecimal(percount)) < 0){
                        //人数
                        BigDecimal bigDecimal = new BigDecimal(percount);
                        BigDecimal total = new BigDecimal("0");
                        PjrecordView pjrecordView2 = new PjrecordView();
                        if(StringUtils.isNotBlank(zhuanye) && new BigDecimal("0").compareTo(new BigDecimal(zhuanye)) < 0){
                            //专业平均分
                            BigDecimal bigDecimal1 = new BigDecimal(zhuanye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setZhuanye(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(chuangxin) && new BigDecimal("0").compareTo(new BigDecimal(chuangxin)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(chuangxin);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setChuangxin(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(gongzuotaidu) && new BigDecimal("0").compareTo(new BigDecimal(gongzuotaidu)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(gongzuotaidu);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setGongzuotaidu(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(xiezuonengli) && new BigDecimal("0").compareTo(new BigDecimal(xiezuonengli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(xiezuonengli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setXiezuonengli(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(zhanlueshiye) && new BigDecimal("0").compareTo(new BigDecimal(zhanlueshiye)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(zhanlueshiye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setZhanlueshiye(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(lingdaoli) && new BigDecimal("0").compareTo(new BigDecimal(lingdaoli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(lingdaoli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setLingdaoli(multiply.toString());
                            total.add(multiply);
                        }
                        pjrecordView2.setZongji(total.toString());
                        pjrecordView2.setBpjpsndocname(bpjpsndocname);
                        pjrecordView2.setBpjrDeptName(view.getBpjrDeptName());
                        hashmap.put(bpjpsndocname,pjrecordView2);
                    }

                }else if("下级".equals(relation) || "平级".equals(relation) || "客户".equals(relation)){
                    //得分除以人数乘以权重
                    String percount = view.getPercount();
                    String zhuanye = view.getZhuanye();
                    String chuangxin = view.getChuangxin();
                    String gongzuotaidu = view.getGongzuotaidu();
                    String xiezuonengli = view.getXiezuonengli();
                    String zhanlueshiye = view.getZhanlueshiye();
                    String lingdaoli = view.getLingdaoli();
                    if(StringUtils.isNotBlank(percount) && new BigDecimal("0").compareTo(new BigDecimal(percount)) < 0){
                        //人数
                        BigDecimal bigDecimal = new BigDecimal(percount);
                        BigDecimal total = new BigDecimal("0");
                        PjrecordView pjrecordView2 = new PjrecordView();
                        if(StringUtils.isNotBlank(zhuanye) && new BigDecimal("0").compareTo(new BigDecimal(zhuanye)) < 0){
                            //专业平均分
                            BigDecimal bigDecimal1 = new BigDecimal(zhuanye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setZhuanye(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(chuangxin) && new BigDecimal("0").compareTo(new BigDecimal(chuangxin)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(chuangxin);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setChuangxin(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(gongzuotaidu) && new BigDecimal("0").compareTo(new BigDecimal(gongzuotaidu)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(gongzuotaidu);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setGongzuotaidu(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(xiezuonengli) && new BigDecimal("0").compareTo(new BigDecimal(xiezuonengli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(xiezuonengli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setXiezuonengli(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(zhanlueshiye) && new BigDecimal("0").compareTo(new BigDecimal(zhanlueshiye)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(zhanlueshiye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setZhanlueshiye(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(lingdaoli) && new BigDecimal("0").compareTo(new BigDecimal(lingdaoli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(lingdaoli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setLingdaoli(multiply.toString());
                            total.add(multiply);
                        }
                        pjrecordView2.setZongji(total.toString());
                        pjrecordView2.setBpjpsndocname(bpjpsndocname);
                        pjrecordView2.setBpjrDeptName(view.getBpjrDeptName());
                        hashmap.put(bpjpsndocname,pjrecordView2);
                    }

                }
            }else{

                String relation = view.getRelation();
                //判断评价关系 根据评价关系计算得分
                if("上级".equals(relation)){
                    //得分除以人数乘以权重
                    String percount = view.getPercount();
                    String zhuanye = view.getZhuanye();
                    String chuangxin = view.getChuangxin();
                    String gongzuotaidu = view.getGongzuotaidu();
                    String xiezuonengli = view.getXiezuonengli();
                    String zhanlueshiye = view.getZhanlueshiye();
                    String lingdaoli = view.getLingdaoli();
                    if(StringUtils.isNotBlank(percount) && new BigDecimal("0").compareTo(new BigDecimal(percount)) < 0){
                        //人数
                        BigDecimal bigDecimal = new BigDecimal(percount);
                        BigDecimal total = new BigDecimal("0");
                        PjrecordView pjrecordView2 = new PjrecordView();
                        if(StringUtils.isNotBlank(zhuanye) && new BigDecimal("0").compareTo(new BigDecimal(zhuanye)) < 0){
                            //专业平均分
                            BigDecimal bigDecimal1 = new BigDecimal(zhuanye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setZhuanye(multiply.toString());
                            total.add(multiply);
                            if (StringUtils.isNotBlank(pjrecordView1.getZhuanye()) && new BigDecimal("0").compareTo(new BigDecimal(pjrecordView1.getZhuanye())) < 0) {
                                total.add(new BigDecimal(pjrecordView1.getZhuanye()));
                            }
                        }
                        if(StringUtils.isNotBlank(chuangxin) && new BigDecimal("0").compareTo(new BigDecimal(chuangxin)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(chuangxin);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setChuangxin(multiply.toString());
                            total.add(multiply);
                            if (StringUtils.isNotBlank(pjrecordView1.getChuangxin()) && new BigDecimal("0").compareTo(new BigDecimal(pjrecordView1.getChuangxin())) < 0) {
                                total.add(new BigDecimal(pjrecordView1.getChuangxin()));
                            }
                        }
                        if(StringUtils.isNotBlank(gongzuotaidu) && new BigDecimal("0").compareTo(new BigDecimal(gongzuotaidu)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(gongzuotaidu);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setGongzuotaidu(multiply.toString());
                            total.add(multiply);
                            if (StringUtils.isNotBlank(pjrecordView1.getGongzuotaidu()) && new BigDecimal("0").compareTo(new BigDecimal(pjrecordView1.getGongzuotaidu())) < 0) {
                                total.add(new BigDecimal(pjrecordView1.getGongzuotaidu()));
                            }
                        }
                        if(StringUtils.isNotBlank(xiezuonengli) && new BigDecimal("0").compareTo(new BigDecimal(xiezuonengli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(xiezuonengli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setXiezuonengli(multiply.toString());
                            total.add(multiply);
                            if (StringUtils.isNotBlank(pjrecordView1.getXiezuonengli()) && new BigDecimal("0").compareTo(new BigDecimal(pjrecordView1.getXiezuonengli())) < 0) {
                                total.add(new BigDecimal(pjrecordView1.getXiezuonengli()));
                            }
                        }
                        if(StringUtils.isNotBlank(zhanlueshiye) && new BigDecimal("0").compareTo(new BigDecimal(zhanlueshiye)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(zhanlueshiye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setZhanlueshiye(multiply.toString());
                            total.add(multiply);
                            if (StringUtils.isNotBlank(pjrecordView1.getChuangxin()) && new BigDecimal("0").compareTo(new BigDecimal(pjrecordView1.getChuangxin())) < 0) {
                                total.add(new BigDecimal(pjrecordView1.getChuangxin()));
                            }
                        }
                        if(StringUtils.isNotBlank(lingdaoli) && new BigDecimal("0").compareTo(new BigDecimal(lingdaoli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(lingdaoli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.4"));
                            pjrecordView2.setLingdaoli(multiply.toString());
                            total.add(multiply);
                            if (StringUtils.isNotBlank(pjrecordView1.getChuangxin()) && new BigDecimal("0").compareTo(new BigDecimal(pjrecordView1.getChuangxin())) < 0) {
                                total.add(new BigDecimal(pjrecordView1.getChuangxin()));
                            }
                        }
                        pjrecordView2.setZongji(total.toString());
                        pjrecordView2.setBpjpsndocname(bpjpsndocname);
                        pjrecordView2.setBpjrDeptName(view.getBpjrDeptName());
                        hashmap.put(bpjpsndocname,pjrecordView2);
                    }

                }else if("下级".equals(relation) || "平级".equals(relation) || "客户".equals(relation)){
                    //得分除以人数乘以权重
                    String percount = view.getPercount();
                    String zhuanye = view.getZhuanye();
                    String chuangxin = view.getChuangxin();
                    String gongzuotaidu = view.getGongzuotaidu();
                    String xiezuonengli = view.getXiezuonengli();
                    String zhanlueshiye = view.getZhanlueshiye();
                    String lingdaoli = view.getLingdaoli();
                    if(StringUtils.isNotBlank(percount) && new BigDecimal("0").compareTo(new BigDecimal(percount)) < 0){
                        //人数
                        BigDecimal bigDecimal = new BigDecimal(percount);
                        BigDecimal total = new BigDecimal("0");
                        PjrecordView pjrecordView2 = new PjrecordView();
                        if(StringUtils.isNotBlank(zhuanye) && new BigDecimal("0").compareTo(new BigDecimal(zhuanye)) < 0){
                            //专业平均分
                            BigDecimal bigDecimal1 = new BigDecimal(zhuanye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setZhuanye(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(chuangxin) && new BigDecimal("0").compareTo(new BigDecimal(chuangxin)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(chuangxin);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setChuangxin(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(gongzuotaidu) && new BigDecimal("0").compareTo(new BigDecimal(gongzuotaidu)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(gongzuotaidu);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setGongzuotaidu(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(xiezuonengli) && new BigDecimal("0").compareTo(new BigDecimal(xiezuonengli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(xiezuonengli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setXiezuonengli(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(zhanlueshiye) && new BigDecimal("0").compareTo(new BigDecimal(zhanlueshiye)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(zhanlueshiye);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setZhanlueshiye(multiply.toString());
                            total.add(multiply);
                        }
                        if(StringUtils.isNotBlank(lingdaoli) && new BigDecimal("0").compareTo(new BigDecimal(lingdaoli)) < 0){
                            BigDecimal bigDecimal1 = new BigDecimal(lingdaoli);
                            BigDecimal divide = bigDecimal1.divide(bigDecimal, BigDecimal.ROUND_HALF_UP, 2);
                            BigDecimal multiply = divide.multiply(new BigDecimal("0.2"));
                            pjrecordView2.setLingdaoli(multiply.toString());
                            total.add(multiply);
                        }
                        pjrecordView2.setZongji(total.toString());
                        pjrecordView2.setBpjpsndocname(bpjpsndocname);
                        pjrecordView2.setBpjrDeptName(view.getBpjrDeptName());
                        hashmap.put(bpjpsndocname,pjrecordView2);
                    }

                }
            }

        }
        List<PjrecordView> list = new ArrayList<>();
        Set<String> strings = hashmap.keySet();
        for (String string : strings) {
            PjrecordView pjrecordView1 = hashmap.get(string);
            list.add(pjrecordView1);
        }
        return list;
    }

    /**
     * 查询评价记录列表
     *
     * @param pjrecordView 评价记录
     * @return 评价记录
     */
    @Override
    public List<PjrecordViewqz> selectPjrecordViewListqz2(PjrecordViewqz pjrecordView)
    {
        List<PjrecordViewqz> pjrecordViews = pjrecordViewqzMapper.selectPjrecordViewqzListqz2(pjrecordView);

        return pjrecordViews;
    }

    /**
     * 新增评价记录
     * 
     * @param pjrecordView 评价记录
     * @return 结果
     */
    @Override
    public int insertPjrecordView(PjrecordView pjrecordView)
    {
        return pjrecordViewMapper.insertPjrecordView(pjrecordView);
    }

    /**
     * 修改评价记录
     * 
     * @param pjrecordView 评价记录
     * @return 结果
     */
    @Override
    public int updatePjrecordView(PjrecordView pjrecordView)
    {
        return pjrecordViewMapper.updatePjrecordView(pjrecordView);
    }

    /**
     * 批量删除评价记录
     * 
     * @param ids 需要删除的评价记录主键
     * @return 结果
     */
    @Override
    public int deletePjrecordViewByIds(Long[] ids)
    {
        return pjrecordViewMapper.deletePjrecordViewByIds(ids);
    }

    /**
     * 删除评价记录信息
     * 
     * @param id 评价记录主键
     * @return 结果
     */
    @Override
    public int deletePjrecordViewById(Long id)
    {
        return pjrecordViewMapper.deletePjrecordViewById(id);
    }
}
